home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BBS in a Box 7
/
BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso
/
Files
/
Prog
/
T
/
TIFF Code.cpt
/
image.c
< prev
next >
Wrap
Text File
|
1987-12-16
|
7KB
|
270 lines
/* Standard C Include files */
/* #include "CType.h" */
/* #include "ErrNo.h" */
/* #include "FCntl.h" */
/* #include "IOCtl.h" */
/* #include "Math.h" */
/* #include "SetJmp.h" */
/* #include "Signal.h" */
/* #include "StdIO.h" */
/* #include "String.h" */
/* Primary Interface Files */
#include "Types.h"
#include "Resources.h"
#include "Quickdraw.h"
#include "Windows.h"
/* #include "OSUtils.h" */
/* Commonly Included files */
/* #include "ToolUtils.h" */
#include "TextEdit.h"
/* #include "Controls.h" */
/* Other Interface files */
/* #include "AppleTalk.h" */
/* #include "CursorCtl.h" */
/* #include "Desk.h" */
/* #include "DeskBus.h" */
/* #include "Devices.h" */
/* #include "Dialogs.h" */
/* #include "DiskInit.h" */
/* #include "Disks.h" */
/* #include "ErrMgr.h" */
/* #include "Errors.h" */
/* #include "Events.h" */
#include "Files.h"
/* #include "Fonts.h" */
/* #include "Graf3D.h" */
/* #include "Lists.h" */
#include "Memory.h"
/* #include "Menus.h" */
/* #include "Packages.h" */
/* #include "Palette.h" */
/* #include "Perf.h" */
/* #include "Picker.h" */
/* #include "Printing.h" */
/* #include "Retrace.h" */
/* #include "ROMDefs.h" */
/* #include "SANE.h" */
#include "Scrap.h"
/* #include "Script.h" */
/* #include "SCSI.h" */
/* #include "SegLoad.h" */
/* #include "Serial.h" */
/* #include "Slots.h" */
/* #include "Sound.h" */
/* #include "Start.h" */
/* #include "Strings.h" */
/* #include "Time.h" */
/* #include "Traps.h" */
/* #include "Values.h" */
/* #include "VarArgs.h" */
/* #include "Video.h" */
/* Application-specific Include files */
#include "::TiffLibrary:TIFFLib.h"
#include "sample.h"
#include "messages.h"
#define MAXCOPYBYTES (3 * 1024)
/*
* ClipPictToOffScreenImage
*
* get PICT from clipboard and write to an offscreen grafport.
*/
void ClipPictToImage(myBitMapPtr)
BitMap *myBitMapPtr;
{
BitMap tempMap;
GrafPtr newPort, oldPort;
Rect rect;
Size mapSize;
short rowBytes;
PicHandle pictureHandle;
OSErr error;
long offset;
if (myBitMapPtr->baseAddr == nil)
MyDisposPtr(&myBitMapPtr->baseAddr);
/* get picture from clipboard */
if ((pictureHandle = MyNewHandle(0L)) == nil) /* get minimum size handle */
return;
if (GetScrap(pictureHandle, 'PICT', &offset) < 0) {
/* notify user that there was nothing to paste */
ErrorMessage(BADCLIP);
return;
}
/* get pictures rect and offset to global coordinates (origin 0,0) */
rect = (**pictureHandle).picFrame;
OffsetRect(&rect, -(rect.left), -(rect.top));
/* set rowbytes, and bit map size */
rowBytes = (((rect.right - rect.left - 1) / (2 * 8)) + 1) * 2;
mapSize = (rect.bottom - rect.top) * rowBytes;
/* set up a bit map */
tempMap.bounds = rect;
tempMap.rowBytes = rowBytes;
if ((tempMap.baseAddr = MyNewPtr(mapSize)) == nil) {
/* notify user that there was a memory error */
DisposHandle(pictureHandle);
return;
}
/* set up a new grafPort to use above bitmap */
if ((newPort = MyNewPtr((Size)sizeof(GrafPort))) == nil) {
/* notify user that there was a memory error */
DisposHandle(pictureHandle);
MyDisposPtr(tempMap.baseAddr);
return;
}
GetPort(&oldPort); /* remember the previous port */
OpenPort(newPort); /* initialize newPort */
SetPort(newPort); /* make current port our own */
SetPortBits(&tempMap); /* replace screenbitmap */
PortSize(rect.right, rect.bottom); /* set newPort's portRect */
RectRgn(newPort->visRgn, &tempMap.bounds); /* set visRgn to bitmap's bounds rect */
/* draw clipboard picture into our new port */
DrawPicture(pictureHandle, &rect);
DisposHandle(pictureHandle);
/* restore previous port */
SetPort(oldPort);
DisposPtr(newPort);
*myBitMapPtr = tempMap;
DisplayImage(FrontWindow(), myBitMapPtr);
return;
}
void ImageToClipPict(myBitMapPtr)
BitMap *myBitMapPtr;
{
PicHandle pictureHandle;
GrafPtr newPort, oldPort;
Rect rect, copyRect;
Size picSize;
Int32 maxCopyRows;
/* set up a new grafPort to create a picture with */
if ((newPort = MyNewPtr((Size)sizeof(GrafPort))) == nil)
return;
GetPort(&oldPort); /* remember the previous port */
OpenPort(newPort); /* initialize newPort */
SetPort(newPort); /* make current port our own */
SetPortBits(myBitMapPtr); /* replace screenbitmap */
rect = myBitMapPtr->bounds;
PortSize(rect.right - rect.left, rect.bottom - rect.top);
RectRgn(newPort->visRgn, &rect); /* set visRgn to bounds */
/* creat pict of our image */
pictureHandle = OpenPicture(&rect);
maxCopyRows = MAXCOPYBYTES / myBitMapPtr->rowBytes;
copyRect = rect;
while (copyRect.top < rect.bottom) {
copyRect.bottom = MIN(copyRect.top + maxCopyRows, rect.bottom);
CopyBits(&newPort->portBits, &newPort->portBits,
©Rect, ©Rect, srcCopy, nil);
copyRect.top = copyRect.bottom;
}
ClosePicture();
/* put it in the scrap */
MoveHHi(pictureHandle);
HLock(pictureHandle);
picSize = GetHandleSize(pictureHandle);
if (ZeroScrap() == noErr)
if (PutScrap(picSize, 'PICT', &(**pictureHandle)) != noErr)
ZeroScrap();
HUnlock(pictureHandle);
KillPicture(pictureHandle);
/* restore previous port */
SetPort(oldPort);
DisposPtr(newPort);
}
void DisplayImage(myWindowPtr, myBitMapPtr)
WindowPtr myWindowPtr;
BitMap *myBitMapPtr;
{
Rect rect,
srcRect,
dstRect,
srcRectStrip,
dstRectStrip;
Int32 maxSrcRows,
maxDstRows,
srcRows,
dstRows,
srcCols,
dstCols,
srcPerDst,
dstPerSrc;
EraseRect(&myWindowPtr->portRect);
if (myBitMapPtr->baseAddr != nil) {
/* initialize some variables */
srcRect = myBitMapPtr->bounds;
dstRect = (qd.thePort)->portRect;
srcRows = srcRect.bottom - srcRect.top;
dstRows = dstRect.bottom - dstRect.top;
srcCols = srcRect.right - srcRect.left;
dstCols = dstRect.right - dstRect.left;
/* maximum number of source image rows less than 3K of memory */
maxSrcRows = MIN(srcRows, (MAXCOPYBYTES / myBitMapPtr->rowBytes));
/* compute ratio of source to destination */
if (srcRows > dstRows) {
srcPerDst = (srcRows + (dstRows - 1)) / dstRows;
maxDstRows = maxSrcRows / srcPerDst;
dstCols = srcCols / srcPerDst; /* same proportions */
}
else {
dstPerSrc = dstRows / srcRows;
maxDstRows = maxSrcRows * dstPerSrc;
dstCols = srcCols * dstPerSrc; /* same proportions */
}
dstRect.right = dstRect.left + dstCols;
/* copy one strip at a time */
srcRectStrip = srcRect;
dstRectStrip = dstRect;
while (srcRectStrip.top < srcRect.bottom) {
srcRectStrip.bottom = srcRectStrip.top + maxSrcRows;
if (srcRectStrip.bottom <= srcRect.bottom)
dstRectStrip.bottom = dstRectStrip.top + maxDstRows;
else { /* last strip, copy whatever is left */
srcRectStrip.bottom = srcRect.bottom;
if (srcRows > dstRows)
dstRectStrip.bottom =
(srcRectStrip.bottom - srcRectStrip.top) / srcPerDst;
else
dstRectStrip.bottom =
(srcRectStrip.bottom - srcRectStrip.top) * dstPerSrc;
}
CopyBits(myBitMapPtr, &(qd.thePort)->portBits,
&srcRectStrip, &dstRectStrip, srcCopy, nil);
srcRectStrip.top = srcRectStrip.bottom;
dstRectStrip.top = dstRectStrip.bottom;
}
}
}
void ClearImage(myBitMapPtr)
BitMap *myBitMapPtr;
{
WindowPtr theActiveWindow;
theActiveWindow = FrontWindow();
EraseRect(&theActiveWindow->portRect);
MyDisposPtr(&myBitMapPtr->baseAddr);
}